package game.module.mj.processor;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.mj.bean.MjRound;
import game.module.mj.logic.MjDataManager;
import game.module.nn.bean.NnRound;
import game.module.nn.logic.NnDataManager;
import game.session.SessionManager;
import io.netty.buffer.ByteBuf;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageMj.C2SMjVoicePlay;
import lua.LuaMessageMj.PushMjVoicePlay;
import lua.LuaMessageMj.S2CMjVoicePlay;

@MsgCodeAnn(msgcode = C2SMjVoicePlay.id, accessLimit = 500)
public class MjVoicPlayProcessor extends PlayingRoleMsgProcessor {
	private static Logger logger = LoggerFactory.getLogger(MjVoicPlayProcessor.class);

	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		C2SMjVoicePlay requset = C2SMjVoicePlay.parse(requestMessage);
		ByteBuf voice_content = requset.content;
		int playerId = playingRole.getId();
		logger.info("mj voice play,playeyId={}", playerId);
		// 房间是否存在
		final MjRound mjRound = MjDataManager.getInstance().getMjRoom(playerId);
		if (mjRound == null) {
			S2ErrorCode retMsg = new S2ErrorCode(201);
			playingRole.writeAndFlush(retMsg.build(playingRole.alloc()));
			return;
		}
		
		S2CMjVoicePlay respMsg = new S2CMjVoicePlay();
		playingRole.writeAndFlush(respMsg.build(playingRole.alloc()));  //发送成功
		
		PushMjVoicePlay pushMsg = new PushMjVoicePlay();
		List<Integer> players = mjRound.getPlayerIds();
		pushMsg.content = voice_content;
		pushMsg.player_index = (byte) players.indexOf(playerId);
		 //推送消息
		for (int i = 0; i < players.size(); i++) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(players.get(i));
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushMsg.build(pr.alloc()));
			}
		}
		
		
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {

	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {

	}
}
